package com.mylib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import androidx.databinding.DataBindingUtil;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mylib.libcore.R;

/**
 * @author Administrator
 * @date 2019/12/20-9:38
 * @description 自定义AppToolBar
 **/
public class AppToolBar extends RelativeLayout {
    private Context mContext;
    private View mView;

    public AppToolBar(Context context) {
        this(context, null);
    }

    public AppToolBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AppToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }
    //定义一个接口对象listerner
    private IvBackClickListener listener;
    //获得接口对象的方法。
    public void setOnIvBackClickListener(IvBackClickListener listener) {
        this.listener = listener;
    }
    //定义一个接口
    public interface  IvBackClickListener{
        public void onClck();
    }

    private void init(Context context, AttributeSet attrs) {
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.tool_bar, this, true);
        TextView tool_bar_tv_title = (TextView) mView.findViewById(R.id.tool_bar_tv_title);
        TextView tool_bar_title_right = (TextView) mView.findViewById(R.id.tool_bar_title_right);
        ImageView  iv_back = (ImageView) mView.findViewById(R.id.tool_bar_iv_back);

        TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.AppToolBar);
        tool_bar_tv_title.setText(a.getString(R.styleable.AppToolBar_toolbar_title_text));
        tool_bar_title_right.setText(a.getString(R.styleable.AppToolBar_toolbar_right_text));

        if(a.getBoolean(R.styleable.AppToolBar_toolbar_show_back,true)){
            iv_back.setVisibility(VISIBLE);
        }else {
            iv_back.setVisibility(GONE);
        }
        iv_back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClck();
            }
        });

        a.recycle();
    }

//    @SuppressLint("ResourceType")
//    public AppToolBar(Context context, AttributeSet attrs) {
//        super(context, attrs);
//        // TODO Auto-generated constructor stub
//        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        mView = inflater.inflate(R.layout.tool_bar, this, true);
//        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.AppToolBar);
//        TextView  titleTv = (TextView) mView.findViewById(R.id.tool_bar_tv_title);
//        titleTv.setText(R.styleable.AppToolBar_title_textw);
////        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.tool_bar, this, true);
////        binding.toolBarTvTitle.setText(R.styleable.AppToolBar_title_text);
//        array.recycle();
//    }

}
